from PySide6.QtWidgets import QMenuBar, QMenu, QMessageBox, QFileDialog
from PySide6.QtGui import QAction
from PySide6.QtCore import Qt


class MenuBar(QMenuBar):
    def __init__(self, main_window):
        super().__init__()
        self.main_window = main_window
        self.initUI()

    def initUI(self):
        # 文件菜单
        fileMenu = self.addMenu('文件')

        newAction = QAction('新建', self.main_window)
        newAction.setShortcut('Ctrl+N')
        newAction.triggered.connect(self.main_window.newProject)
        fileMenu.addAction(newAction)

        openAction = QAction('打开', self.main_window)
        openAction.setShortcut('Ctrl+O')
        openAction.triggered.connect(self.main_window.openProject)
        fileMenu.addAction(openAction)

        saveAction = QAction('保存', self.main_window)
        saveAction.setShortcut('Ctrl+S')
        saveAction.triggered.connect(self.main_window.saveProject)
        fileMenu.addAction(saveAction)

        saveAsAction = QAction('另存为', self.main_window)
        saveAsAction.setShortcut('Ctrl+Shift+S')
        saveAsAction.triggered.connect(self.main_window.saveAsProject)
        fileMenu.addAction(saveAsAction)

        fileMenu.addSeparator()

        exitAction = QAction('退出', self.main_window)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(self.main_window.close)
        fileMenu.addAction(exitAction)

        # 编辑菜单
        editMenu = self.addMenu('编辑')

        copyAction = QAction('复制', self.main_window)
        copyAction.setShortcut('Ctrl+C')
        copyAction.triggered.connect(self.main_window.copySelected)
        editMenu.addAction(copyAction)

        cutAction = QAction('剪切', self.main_window)
        cutAction.setShortcut('Ctrl+X')
        cutAction.triggered.connect(self.main_window.cutSelected)
        editMenu.addAction(cutAction)

        pasteAction = QAction('粘贴', self.main_window)
        pasteAction.setShortcut('Ctrl+V')
        pasteAction.triggered.connect(self.main_window.pasteSelected)
        editMenu.addAction(pasteAction)

        deleteAction = QAction('删除', self.main_window)
        deleteAction.setShortcut('Del')
        deleteAction.triggered.connect(self.main_window.deleteSelected)
        editMenu.addAction(deleteAction)

        # 全局菜单
        globalMenu = self.addMenu('全局')

        runAction = QAction('运行', self.main_window)
        runAction.setShortcut('F5')
        runAction.triggered.connect(self.main_window.runProcessing)
        globalMenu.addAction(runAction)

        stopAction = QAction('终止', self.main_window)
        stopAction.setShortcut('F6')
        stopAction.triggered.connect(self.main_window.stopProcessing)
        globalMenu.addAction(stopAction)

        resetAction = QAction('复位', self.main_window)
        resetAction.setShortcut('F7')
        resetAction.triggered.connect(self.main_window.resetProcessing)
        globalMenu.addAction(resetAction)

        # 连接菜单 (原子系统菜单)
        connectionMenu = self.addMenu('连接')

        clearConnectionsAction = QAction('清除连接', self.main_window)
        clearConnectionsAction.triggered.connect(self.main_window.clearConnections)
        connectionMenu.addAction(clearConnectionsAction)

        # 添加检查动作
        checkAction = QAction('检查', self.main_window)
        checkAction.triggered.connect(self.main_window.checkConnections)
        connectionMenu.addAction(checkAction)

        # 添加统计动作
        statsAction = QAction('统计', self.main_window)
        statsAction.triggered.connect(self.main_window.showStatistics)
        connectionMenu.addAction(statsAction)

        # 主题菜单
        themeMenu = self.addMenu('主题')

        lightThemeAction = QAction('白天主题', self.main_window)
        lightThemeAction.triggered.connect(lambda: self.main_window.applyTheme("light"))
        themeMenu.addAction(lightThemeAction)

        darkThemeAction = QAction('黑夜主题', self.main_window)
        darkThemeAction.triggered.connect(lambda: self.main_window.applyTheme("dark"))
        themeMenu.addAction(darkThemeAction)

        # 🔧 新增：关于菜单
        aboutMenu = self.addMenu('关于')

        aboutAction = QAction('关于此程序', self.main_window)
        aboutAction.triggered.connect(self.showAboutDialog)
        aboutMenu.addAction(aboutAction)

    def showAboutDialog(self):
        """显示关于对话框"""
        try:
            from dialogs.about_dialog import AboutDialog
            dialog = AboutDialog(self.main_window)
            # 设置当前主题
            dialog.setTheme(self.main_window.currentTheme)
            dialog.exec_()  # 模态显示
        except ImportError as e:
            # 如果导入失败，使用简单的QMessageBox作为回退
            QMessageBox.about(self.main_window,
                            "关于音频软件定义处理器",
                            "音频软件定义处理器\n\n版本 1.0\n\n基于 PySide6 和 Python 开发的音频处理应用程序。")